package com.wjv.example.spbt.task.controller;

import com.wjv.example.spbt.task.aop.AopDemo;
import com.wjv.example.spbt.task.aop.QueryParams;
import com.wjv.example.spbt.task.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.crypto.interfaces.PBEKey;
import java.util.concurrent.ExecutionException;

/**
 * <p>Title: TaskController</p>
 *
 * @author wjv
 * @since 2022/2/7 11:41
 */
@RestController
@RequestMapping("task")
public class TaskController {

    @Autowired
    private TaskService taskService;

    @GetMapping("/task")
    public Object task() throws InterruptedException {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("串行执行");
        Object result = taskService.task();
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
        return result;
    }

    @GetMapping("/parallelTask")
    public Object parallelTask() throws InterruptedException, ExecutionException {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("并行执行");
        Object result = taskService.parallelTask();
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
        return result;
    }

    @Autowired
    AopDemo aopDemo;

    @GetMapping("/echo")
    public String echo() {
        QueryParams params = new QueryParams();
        params.setId(10);
        params.setName("参数11");
        aopDemo.echo(params);
        return "hello";
    }
}
